import os
from PyQt4 import QtCore, QtGui

class Settings(QtGui.QDialog):
    def __init__(self, parent = None):
        QtGui.QDialog.__init__(self)
        self.parent = parent
        
        self.cuts = None
        self.game_time = None
        
        ## Try to load saved settings, else use defaults
        if 'conf.dat' in os.listdir('.'):
            self.loadSettings()
        else:
            self.loadDefaultSettings()
            
        self.cuts_changed = False
        self.time_changed = False
            
        ## Hard-coded settings
        self.version = 1.0
        self.win_width, self.win_height = 1024,768
        self.im_width, self.im_height = 865,670
        
        ## Window options
        self.setWindowTitle('Unshuffler Options')
        self.resize(225,250)
            
        ## and the widgets that go with them...
        self.cuts_spbox = QtGui.QSpinBox(self)
        self.cuts_spbox.setMinimum(1)
        self.cuts_spbox.setMaximum(75)
        self.cuts_spbox.setValue(self.cuts)
        self.game_time_spbox = QtGui.QSpinBox()
        self.game_time_spbox.setMinimum(1)
        self.game_time_spbox.setMaximum(59)
        self.game_time_spbox.setValue(self.game_time[2])
            
        ## Create dialog layout
        vbox = QtGui.QVBoxLayout()
        
        ## Spinners
        subdiv_hbox = QtGui.QHBoxLayout()
        subdiv_hbox.addWidget(QtGui.QLabel("Number of Subdivisions:"))
        subdiv_hbox.addStretch()
        subdiv_hbox.addWidget(self.cuts_spbox)
        vbox.addLayout(subdiv_hbox)
        gametime_hbox = QtGui.QHBoxLayout()
        gametime_hbox.addWidget(QtGui.QLabel("Seconds in Game:"))
        gametime_hbox.addStretch()
        gametime_hbox.addWidget(self.game_time_spbox)
        vbox.addLayout(gametime_hbox)
        
        vbox.addStretch()
        
        ## Buttons
        self.save_btn = QtGui.QPushButton('Save')
        self.load_btn = QtGui.QPushButton('Load')
        self.apply_btn = QtGui.QPushButton('Apply')
        self.cancel_btn = QtGui.QPushButton('Cancel')
        buttons_hbox = QtGui.QHBoxLayout()
        buttons_hbox.addWidget(self.save_btn)
        buttons_hbox.addWidget(self.load_btn)
        buttons_hbox.addStretch()
        buttons_hbox.addWidget(self.apply_btn)
        buttons_hbox.addWidget(self.cancel_btn)
        vbox.addLayout(buttons_hbox)
        
        self.setLayout(vbox)
        
        ## Create connections
        #self.connect(self.save_btn, QtCore.SIGNAL('valueChanged()'), self.setReloadGame)
        self.connect(self.cuts_spbox, QtCore.SIGNAL('valueChanged(int)'), self.setCuts)
        self.connect(self.game_time_spbox, QtCore.SIGNAL('valueChanged(int)'), self.setTime)
        
        self.connect(self.save_btn, QtCore.SIGNAL('clicked()'), self.saveSettings)
        self.connect(self.load_btn, QtCore.SIGNAL('clicked()'), self.loadSettings)
        self.connect(self.apply_btn, QtCore.SIGNAL('clicked()'), self.applySettings)
        self.connect(self.cancel_btn, QtCore.SIGNAL('clicked()'), self.cancelSettings)
        
    def applySettings(self):
        if self.cuts_changed:
            self.cuts_changed = False
            self.parent.game_frame.initGame(overlay=True)
        self.close()
    
    def cancelSettings(self):
        if self.time_changed:
            self.setTime(self.old_time)
            self.time_changed = False
            self.game_time_spbox.setValue(self.old_time)
        if self.cuts_changed:
            self.cuts = self.old_cuts
            self.cuts_changed = False
            self.cuts_spbox.setValue(self.old_cuts)
        self.close()
        
    def loadDefaultSettings(self):
        self.image = 'images/awesome_face.jpg'
        self.cuts = 1
        self.game_time = (0,0,10) # (H,M,S)
        
    def loadSettings(self):
        try:
            with open('conf.dat','r') as fp:
                self.image = fp.readline().strip()
                self.cuts = int(fp.readline().strip())
                self.game_time = (0,0,int(fp.readline().strip()))
                self.parent.status_bar.showMessage('Successfully loaded conf.dat', 3500)
        except Exception:
            self.loadDefaultSettings()
    
    def saveSettings(self):
        with open('conf.dat','w') as fp:
            fp.write(str(self.image)+'\n')
            fp.write(str(self.cuts)+'\n')
            fp.write(str(self.game_time[2])+'\n')
            self.parent.status_bar.showMessage('Saved settings to conf.dat', 3500)
            
    def setCuts(self, val):
        if not self.cuts_changed:
            self.old_cuts = self.cuts
            self.cuts_changed = True
            
        self.cuts = val
        
    def setTime(self, val):
        if not self.time_changed:
            self.old_time = self.game_time[2]
            self.time_changed = True
            
        self.game_time = (0,0,int(str(val)))
        self.parent.game_frame.controls.timer.reset()